package cn.itcast.bos.web.service.take_delivery.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.web.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {

	@Autowired
	private WayBillRepository wayBillRepository;
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Override
	public void save(WayBill wayBill) {
		WayBill presistWayBill = wayBillRepository.findByWayBillNum(wayBill.getWayBillNum());
		System.err.println(wayBill.getWayBillNum());
		if (presistWayBill == null || presistWayBill.getId() == null) {
			wayBill.setSignStatus(1);
			wayBillRepository.save(wayBill);
			wayBillIndexRepository.save(wayBill);
		} else {
			try {
				if(presistWayBill.getSignStatus()==1){
					Integer id = presistWayBill.getId();
					BeanUtils.copyProperties(presistWayBill, wayBill);
					presistWayBill.setId(id);
					presistWayBill.setSignStatus(1);
					wayBillIndexRepository.save(presistWayBill);	
				}else{
					throw new RuntimeException("运单已发出，无法修改保存！！");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}

	}

	@Override
	public Page<WayBill> findPageData(WayBill wayBill, Pageable pageable) {

		if (StringUtils.isBlank(wayBill.getWayBillNum()) 
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)
				&& StringUtils.isBlank(wayBill.getSendProNum())) {
			System.err.println("无条件查询");
			return wayBillRepository.findAll(pageable);
		} else {
			BoolQueryBuilder query = new BoolQueryBuilder();
			
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				//订单号 等值查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				//发货地查询 模糊查询
				//情况一
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				
				//情况二：输入的字符长度过长
				QueryBuilder queryString = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddRess")
						.defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryString);
				
				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				//收货地查询 模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				
				//情况二：输入的字符长度过长
				QueryBuilder queryString = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("recAddRess")
						.defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryString);
				
				query.must(boolQueryBuilder);
				query.must(wildcardQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				//产品类型
				QueryBuilder tempQuery = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				query.must(tempQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				QueryBuilder tempQuery = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
				query.must(tempQuery);
			}
			System.out.println(query+"QUERY...");
			SearchQuery searchQuery = new NativeSearchQuery(query);
			searchQuery.setPageable(pageable);
			return wayBillIndexRepository.search(searchQuery);
		}
	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		// TODO Auto-generated method stub
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}

	@Override
	public void syncIndex() {
		List<WayBill> wayBills = wayBillRepository.findAll();
		
		wayBillIndexRepository.save(wayBills);
		
	}

	@Override
	public List<WayBill> findWayBills(WayBill wayBill) {
		if (StringUtils.isBlank(wayBill.getWayBillNum()) 
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)
				&& StringUtils.isBlank(wayBill.getSendProNum())) {
			System.err.println("无条件查询");
			return wayBillRepository.findAll();
		} else {
			BoolQueryBuilder query = new BoolQueryBuilder();
			
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				//订单号 等值查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				//发货地查询 模糊查询
				//情况一
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				
				//情况二：输入的字符长度过长
				QueryBuilder queryString = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddRess")
						.defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryString);
				
				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				//收货地查询 模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				
				//情况二：输入的字符长度过长
				QueryBuilder queryString = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("recAddRess")
						.defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryString);
				
				query.must(boolQueryBuilder);
				query.must(wildcardQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				//产品类型
				QueryBuilder tempQuery = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				query.must(tempQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				QueryBuilder tempQuery = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
				query.must(tempQuery);
			}
			System.out.println(query+"QUERY...");
			SearchQuery searchQuery = new NativeSearchQuery(query);
			Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
			searchQuery.setPageable(pageable);
			return wayBillIndexRepository.search(searchQuery).getContent();
		}
	}

}
